<template>
	<coreshop-page title="公告列表" mode="left">
		<view class="layout-notice-box">
			<view v-if="state.list.length > 0">
				<view class="item-box" v-for="item in state.list" :key="item.id"
					@click="handleRouteNavigateTo(`/pages/subpackage/notice/detail/detail?id=${item.id}`)">
					<view class="title">{{ item.title }}</view>
					<uv-icon name="arrow-right" size="18"></uv-icon>
				</view>
				<view class="no-data" v-if="state.noData">没有更多了</view>
			</view>
			<view v-else class="layout-empty-box">
				<coreshop-empty :mode="EmptyEnum.data" text="暂无数据"></coreshop-empty>
			</view>
		</view>

	</coreshop-page>
</template>
<script setup lang="ts">
	import { reactive, onMounted } from 'vue';
	import { onReachBottom } from '@dcloudio/uni-app';
	import { queryNoticeList } from '@/core/api';
	import type { Response, NoticeType } from '@/core/models';
	import { EmptyEnum } from '@/core/enum';
	import { handleRouteNavigateTo } from '@/core/utils';

	const state = reactive<{
		list : Array<NoticeType>;
		page : number;
		limit : number;
		noData : boolean;
	}>({
		list: [],
		page: 1,
		limit: 10,
		noData: false,
	})

	onMounted(() => {
		/** 获取公告列表 */
		getNoticeList()
	});

	/** 获取公告列表 */
	const getNoticeList = async () => {
		uni.showLoading({
			title: "加载中..."
		})
		const noticeList : Response<Array<NoticeType>> = await queryNoticeList({
			page: state.page,
			limit: 10,
		});
		if (state.limit > noticeList.data?.length || noticeList.data?.length == 0) {
			state.noData = true;
		}
		if (noticeList.data?.length > 0) {
			state.list = state.list.concat(noticeList.data);
		}
		uni.hideLoading();
	}

	onReachBottom(() => {
		if (!state.noData) {
			state.page++;
			getNoticeList();
		}
	});
</script>
<style lang="scss" scoped>
	@import './list.scss';
</style>